package com.fly.web;

import com.fly.base.Result;
import com.fly.base.ResultGenerator;
import com.fly.domain.model.CustomerCardInfo;
import com.fly.domain.service.CardInfoService;
import com.fly.domain.service.CustomerCardInfoService;
import com.fly.domain.service.CustomerService;
import com.fly.util.SidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.util.Optional;

import static com.fly.util.SidService.Biz.CUSTOMER;

/**
 * @author frinder
 * @date 2018/5/19
 * @className MyCustomerCardInfoController
 * @package com.fly.web
 * @Description: ${todo}
 */
@RestController
@RequestMapping("/myCustomerCardInfo")
public class MyCustomerCardInfoController {

    @Autowired
    private CustomerService customerService;

    @Resource
    private CustomerCardInfoService customerCardInfoService;

    @Autowired
    private CardInfoService cardInfoService;

    @Autowired
    private SidService sidService;

    @PostMapping("/add")
    public Result add(CustomerCardInfo customerCardInfo) {
        Optional.ofNullable(customerService.findBy("sid", customerCardInfo.getCustomerSid()))
                .ifPresent(s -> {
                    customerCardInfo.setCustomerName(s.getName());
                    customerCardInfo.setSid(sidService.getSid(CUSTOMER));
                    int type = customerCardInfo.getType();
                    if (1 == type) {
                        Optional.ofNullable(cardInfoService.findBy("sid", customerCardInfo.getCardSid()))
                                .ifPresent(c -> {
                                    customerCardInfo.setCardSid(c.getSid());
                                    customerCardInfo.setCardName(c.getName());
                                    customerCardInfo.setQty(c.getQty());
                                    customerCardInfo.setRemainQty(c.getQty());
                                    customerCardInfo.setCash(c.getSalesPrice());
                                });
                        customerCardInfo.setRemainQty(customerCardInfo.getQty());
                    } else {
                        Optional.ofNullable(customerCardInfoService.findBy("sid", customerCardInfo.getParentSid()))
                                .ifPresent(c -> {
                                    customerCardInfo.setCardSid(c.getCardSid());
                                    customerCardInfo.setCardName(c.getCardName());
                                    customerCardInfo.setOfferSid(c.getOfferSid());
                                    customerCardInfo.setRemainQty(c.getRemainQty() - 1);
                                    customerCardInfo.setCustomerSid(c.getCustomerSid());
                                    customerCardInfo.setCash(c.getCash() / c.getQty());
                                    //
                                    CustomerCardInfo updateCustomerCardInfo = new CustomerCardInfo();
                                    updateCustomerCardInfo.setId(c.getId());
                                    updateCustomerCardInfo.setRemainQty(c.getRemainQty() - 1);
                                    customerCardInfoService.update(updateCustomerCardInfo);
                                });
                    }
                    customerCardInfoService.save(customerCardInfo);
                });
        return ResultGenerator.genSuccessResult();
    }

}
